//
//  HMNewGuideViewController.m
//  新特性
//
//  Created by mac on 17/1/5.
//  Copyright © 2017年 mac.gu. All rights reserved.
//

#import "HMNewGuideViewController.h"
#import "HMNewGuideCollectionViewCell.h"
#import "HMNewGuideVIewFlowLayout.h"
#import "Masonry.h"
#import "HMMainViewController.h"
#import "HMPhoneNumLoginViewController.h"
#import "HMPhoneNumSignInViewController.h"

@interface HMNewGuideViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>


@end

@implementation HMNewGuideViewController{
    UIPageControl *_pageControl;
    UIButton *_thirdButton;
    UIButton *_weChatButton;
}
static NSString *newGuideCellId = @"newGuideCellId";
#define kImageCount 3
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"返回";
    self.navigationItem.backBarButtonItem = backItem;
    
    [self creatCollectionView];
    
    [self creatPageControl];
    [self creatLogInButton];
    
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

//创建按钮登录
- (void)creatLogInButton{
    
    //创建微信登录按钮
    _weChatButton = [[UIButton alloc]init];
    [self.view addSubview:_weChatButton];
    
    [_weChatButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view);
        make.width.offset(300);
        make.height.offset(40);
        make.top.equalTo(_pageControl.mas_bottom).offset(5);
    }];
    
    [_weChatButton setTitle:@"微信快捷登录" forState:UIControlStateNormal];
    
//    [_weChatButton.imageView setImage:[UIImage imageNamed:@"new_account_wechat_25x25_"]];
    
    [_weChatButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [_weChatButton setBackgroundImage:[UIImage imageNamed:@"login_bg"] forState:UIControlStateNormal];
    
    [_weChatButton setBackgroundImage:[UIImage imageNamed:@"login_bg"] forState:UIControlStateHighlighted];
    
    [_weChatButton addTarget:self action:@selector(weChatBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    NSArray *titleArray = @[@"手机登录",@"手机注册",@"随便看看"];
    //创建分类按钮
    NSMutableArray *buttonArrM = [NSMutableArray arrayWithCapacity:3];
    
    for (NSInteger i = 0; i < titleArray.count; i++) {
        
        UIButton *titleButton = [[UIButton alloc]init];
        [titleButton setTitle:titleArray[i] forState:UIControlStateNormal];
        
        [titleButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        
        titleButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        //给button设置tag
        titleButton.tag = i;
        
        
        [titleButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

        
        [self.view addSubview:titleButton];
        
        [buttonArrM addObject:titleButton];
        
    }
    _thirdButton = buttonArrM[2];
    [_thirdButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    
    //设置按钮的约束
    [buttonArrM mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:-50 leadSpacing:0 tailSpacing:0];
    
    
    [buttonArrM mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-50);
    }];
    
    
    
}
//微信按钮点击事件
- (void)weChatBtnClick{
    
}



//注册 登录 按钮 点击事件
- (void)buttonClick:(UIButton *)sender{
    if (sender.tag == 0) {
        //手机登录
        HMPhoneNumLoginViewController *loginVC = [[HMPhoneNumLoginViewController alloc] init];
        //  新特性
        loginVC.isGuide = YES;
        [self.navigationController pushViewController:loginVC animated:YES];
        
    }else if (sender.tag == 1){

        //手机注册
        HMPhoneNumSignInViewController *signInVC = [[HMPhoneNumSignInViewController alloc] init];
        //  新特性
        signInVC.isGuide = YES;
        [self.navigationController pushViewController:signInVC animated:YES];
        
    }else{
        //随便看看
        
        HMMainViewController * mainVC = [[HMMainViewController alloc] init];
        
        //获取应用程序的对象
        UIApplication * app = [UIApplication sharedApplication];
        
        app.keyWindow.rootViewController = mainVC;
        
    }
    
    
}


//创建pageControl
- (void)creatPageControl{
    
    _pageControl = [[UIPageControl alloc]init];
    [self.view addSubview:_pageControl];
    _pageControl.numberOfPages = kImageCount;
    _pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:1];
    _pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    _pageControl.enabled = NO;
    
    _pageControl.currentPage = 0;
    [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.offset(-135);
        make.centerX.equalTo(self.view);
        
    }];
    
}


//创建collectionView
- (void)creatCollectionView{
    
    HMNewGuideVIewFlowLayout *flowLayout = [[HMNewGuideVIewFlowLayout alloc] init];
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    collectionView.bounces = NO;
    [collectionView registerClass:[HMNewGuideCollectionViewCell class] forCellWithReuseIdentifier:newGuideCellId];
    
    collectionView.showsHorizontalScrollIndicator = NO;
    
    collectionView.pagingEnabled = YES;
    
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return kImageCount;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HMNewGuideCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:newGuideCellId forIndexPath:indexPath];
    
    cell.indexPath = indexPath;
    
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger cellNum = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.499;
    
    NSInteger page = cellNum % kImageCount;
    
    _pageControl.currentPage = page;
    
}


@end
